<template>
  <div>
    <div class="reg">
      <div class="top">
        <a href>
          <img src="../assets/milogo.jpg" alt />
        </a>
      </div>
      <div class="reg-bg-img">
        <a href>
          <img src="../assets/quanping.jpg" alt />
        </a>
      </div>
      <div class="reg-main">
        <div>
          <img src="../assets/logo.jpg" alt />
        </div>
        <h1>注册小米账号</h1>
        <div>
          <div>
            <span>邮箱:</span>
            <input type="text" placeholder="请输入您的邮箱" v-model="uname" @blur="checkUname" />
            <p v-show="unameErr==undefined"></p>
            <p v-show="unameErr==0">邮箱不能为空</p>
            <p v-show="unameErr==1" class="ok">邮箱格式正确</p>
            <p v-show="unameErr==2">邮箱格式错误</p>
          </div>

          <div>
            <span>密码:</span>
            <input type="password" placeholder="请输入密码" v-model="upwd" @blur="checkUpwd" />
            <p v-show="upwdErr==0">密码不能为空</p>
            <p v-show="upwdErr==1" class="ok">密码格式正确</p>
            <p v-show="upwdErr==2">密码为6~12位字母或数字</p>
          </div>

          <div>
            <span>+86:</span>
            <input type="text" placeholder="请输入您的手机号" v-model="phone" @blur="checkPhone" />
            <p v-show="phoneErr==0">手机号不能为空</p>
            <p v-show="phoneErr==1" class="ok">手机号格式正确</p>
            <p v-show="phoneErr==2">请填写正确手机号</p>
          </div>
          <div class="captcha">
            <span>验证码：</span>
            <input v-model="captcha" type="text" placeholder="请输入验证码" />
            
            <img :src="captchaURL" />
            <strong @click="randomNum=Math.random()" class="iconfont iconshuaxin"></strong>
          </div>
        </div>
        <a href="javascript:;" @click="doRegister">立即注册</a>
      </div>
      <div class="bottom">
        <ul>
          <li>
            <a href>简体</a>
          </li>
          <li>
            <a href>繁体</a>
          </li>
          <li>
            <a href>English</a>
          </li>
          <li>
            <a href>常见问题</a>
          </li>
          <li>
            <a href>隐私政策</a>
          </li>
        </ul>
        <p>小米公司版权所有-京ICP备10046444-京公网安备11010802020134号-京ICP证110507号</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      uname: "",
      upwd: "",
      phone: "",
      unameErr: undefined, //规定0表示为空 1表示格式正确 2表示格式错误
      upwdErr: undefined, //规定0表示为空 1表示格式正确 2表示格式错误
      phoneErr: undefined,//规定0表示为空 1表示格式正确 2表示格式错误
      randomNum: Math.random(),
      captcha: '',
    };
  },
  computed: {
    captchaURL() {
      return (
        this.$http.defaults.baseURL + "/captcha/register?" + this.randomNum
      );
    }
  },
  methods: {
    checkUname() {
      if (this.uname == "") {
        this.unameErr = 0;
      } else {
        let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
        if (reg.test(this.uname)) {
          this.unameErr = 1;
        } else {
          this.unameErr = 2;
        }
      }
    },
    checkUpwd() {
      if (this.upwd == "") {
        this.upwdErr = 0;
      } else {
        let reg = (reg = /^[0-9a-zA-Z]{6,12}$/);
        if (reg.test(this.upwd)) {
          this.upwdErr = 1;
        } else {
          this.upwdErr = 2;
        }
      }
    },
    checkPhone() {
      if (this.phone == "") {
        this.phoneErr = 0;
      } else {
        let reg = /^[1]([3-9])[0-9]{9}$/;
        if (reg.test(this.phone)) {
          this.phoneErr = 1;
        } else {
          this.phoneErr = 2;
        }
      }
    },
    doRegister() {
      // console.log( req.session.registerCaptcha);
      //收集表单数据，向服务器提交异步POST请求(Content-Type: application/json)
      let url = "/user/register";
      let data = {
        uname: this.uname,
        upwd: this.upwd,
        phone: this.phone,
        captcha: this.captcha
      };
      let config = {
        /*headers: {
				'Content-Type': 'application/json'
			}*/
      };

      this.$http.post(url, data, config).then(res => {
        console.log(res.data);
        
        if (res.data.code === 200) {
          alert("新用户注册成功！点击确定跳转到登录页");
          this.$router.push("/login");
        } else {
          alert("注册失败！错误消息：" + res.data.msg);
        }
        if(res.data.code===410){
          alert('验证码有误,请重新输入')
        }
      });
    }
  }
};
</script>

<style scoped>
.captcha{
  margin: 10px;
}
.captcha>input{
  width: 100px !important;
  margin-left: 0px !important;
}
.ok {
  color: green !important;
}
.iconshuaxin {
  font-size: 30px;
}
* {
  margin: 0;
  padding: 0;
}
a {
  text-decoration: none;
}
ul {
  list-style: none;
}
.reg {
  width: 1500px;
  height: 600px;
  margin: 0 auto;
  position: relative;
}
.reg > .top {
  width: 1130px;
  height: 49px;
  margin: 40px auto 20px;
}
.reg > reg-bgimg > a {
  display: block;
}
.reg > .reg-main {
  width: 430px;
  height: 585px;
  margin: 0 auto;
  background-color: #fff;
  position: absolute;
  top: 100px;
  left: 350px;
}
.reg > .reg-main > div:first-child {
  text-align: center;
  margin-top: 15px;
}
.reg > .reg-main > h1 {
  margin-top: 20px;
  text-align: center;
  font-weight: normal;
}
.reg > .reg-main > div:nth-child(3) {
  width: 85%;
  margin: 0 auto;
  margin-top: 70px;
}
.reg > .reg-main > div:nth-child(3) > div {
  width: 100%;
}
.reg > .reg-main > div:nth-child(3) > div > span {
  font-size: 14px;
  color: #666;
}
.reg > .reg-main > div:nth-child(3) > div > input {
  outline: none;
  width: 78%;
  height: 38px;
  margin-left: 22px;
  color: #666;
  padding-left: 8px;
  margin-top: 7px;
}
.reg > .reg-main > div:nth-child(3) > div > p {
  margin: 12px 0 11px 53px;
  font-size: 14px;
  color: #f66;
}
.reg > .reg-main > a {
  display: block;
  width: 299px;
  height: 42px;
  line-height: 35px;
  text-align: center;
  margin-left: 85px;
  color: #fff;
  background-color: #ff6700;
}

.reg > .bottom {
  width: 800px;
  margin: 0 auto;
}
.bottom > ul {
  width: 340px;
  display: flex;
  margin: 15px auto;
}
.bottom > ul > li > a,
.bottom > p {
  color: #757575;
  font-size: 14px;
  text-align: center;
}
.bottom > p {
  margin-bottom: 50px;
}
.bottom > ul > li + li::before {
  content: "|";
  display: inline;
  margin: 8px;
  color: #757575;
  font-size: 12px;
}
</style>